/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.common.core.controller;

import cn.hutool.core.util.StrUtil;
import com.zxp.common.core.api.R;
import com.zxp.common.core.api.SystemCode;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author: shollin
 * @date: 2021/7/6/006 10:28
 */
public abstract class BaseController implements IController{

    // beetl的后缀，如果是beetl视图，则需要加上.html

    @Value("${beetl.suffix:}")
    protected String beetlSuffix;

    /**
     * 根据状态返回成功和失败
     *
     * @param status 状态
     * @param <T>    枚举
     * @return 返回信息
     */
    protected <T> R<T> status(boolean status) {
        return status ? success() : fail();
    }

    /**
     * 状态返回失败
     *
     * @param <T> 枚举
     * @return 返回信息
     */
    protected <T> R<T> fail() {
        return R.fail(SystemCode.FAILURE, "操作失败");
    }

    protected String view(String viewPath){
        if(StrUtil.isNotBlank(beetlSuffix)){
            return StrUtil.subBefore(viewPath,".",true).concat(".").concat(beetlSuffix);
        }
        return viewPath;
    }
}
